<?php
namespace gnomephp\crud;

use \gnomephp\doctrine\Doctrine;

/**
 * Crud class specific to Doctrine.
 * This Controller class lets you implement CRUD to your application easily.
 *
 * CRUD stands for Create , Read , Update and Delete. Your entities can easily be managed with this manager.
 *
 * Override this Controller in your Application and you might want to override the __construct to have some security check implementation to check if the user is admin.
 *
 * @author peec
 *
 */
abstract class CRUD_Doctrine extends \gnomephp\mvc\CoreController{


	public function __construct(){
		parent::__construct();
		Doctrine::load();
	}

	/**
	 * Shows all the entities.
	 */
	public function index(){
		$em = Doctrine::getEM();
		// Get metadata for all entities.
		$metadatas = $em->getMetadataFactory()->getAllMetadata();


		$entities = array();

		// Foreach entities ..
		foreach($metadatas as $metadata){
			$n = $metadata->name;
			$refClass = new \ReflectionClass($n);
				
			if (in_array('gnomephp\crud\CRUD_Entity', $refClass->getInterfaceNames())){
			
				// Replace ns separator to "-" for nice url.
				$entities[$metadata->name] = str_replace(array('\\','/'), '-', $metadata->name);
			}
		}

		$this->view->assign('entities', $entities);

		$this->view->assign('methodLink', $this->view->url->link($this->getClassName($this). '/index'));

		$this->view->render('crud/index');


	}


	/**
	 * Shows a specific entitiy.
	 * @param string $entity identifier for the entity.
	 */
	public function show($entity){
		$this->view->assign('entity', $entity);

		// Rereplace to real namespace.
		$entity = str_replace('-', '\\', $entity);
		$worker = new DoctrineEntity(Doctrine::getEM(), $entity);
		

		$this->view->assign('fields', $worker->getFields());
		$this->view->assign('entries', $worker->getEntries());
		
		$this->view->assign('worker', $worker);

		$this->view->render('crud/show');


	}

	public function read($entity, $id){

		$this->view->assign('entity', $entity);
		$this->view->assign('id', $id);
		// Rereplace to real namespace.
		$entity = str_replace('-', '\\', $entity);
		$worker = new DoctrineEntity(Doctrine::getEM(), $entity);
		

		$this->view->assign('fields', $worker->getFields());
		$this->view->assign('relations', $worker->getRelations($id));
		
		$this->view->assign('entry', $worker->getEntryWhereId($id));
		$this->view->assign('worker', $worker);
		
		$this->view->render('crud/read');

	}

	public function update($entity, $id){
		// Rereplace to real namespace.
		$entity_c = str_replace('-', '\\', $entity);



		if ($this->input->post){

			$vars = $this->input->post->get();
			$worker = new DoctrineEntity(Doctrine::getEM(), $entity_c);
			$worker->updateEntry($id,$vars);
				

			\gnomephp\message\Message::add(\gnomephp\message\Message::SUCCESS, "Successfully updated entry.");
		}



		$this->read($entity, $id);

	}


	public function delete($entity, $id){
		$c_entity = $entity;
		// Rereplace to real namespace.
		$entity = str_replace('-', '\\', $entity);
		$worker = new DoctrineEntity(Doctrine::getEM(), $entity);
		$worker->deleteEntry($id);
		$this->view->assign('worker', $worker);
		
		\gnomephp\message\Message::add(\gnomephp\message\Message::SUCCESS, "Successfully deleted entry.");

		$this->redirect($this->view->url->linkTo('crud_entity', array('name' => $c_entity)));
	}



	public function create($entity){
		$c_entity = $entity;
		$this->view->assign('entity', $entity);
		// Rereplace to real namespace.
		$entity = str_replace('-', '\\', $entity);
		$worker = new DoctrineEntity(Doctrine::getEM(), $entity);

		$this->view->assign('fields', $worker->getFields());
		$this->view->assign('worker', $worker);
		
		if ($this->input->post){
			try{
				$data = $this->input->post->get();
				$worker->createEntry($data);
					
				\gnomephp\message\Message::add(\gnomephp\message\Message::SUCCESS, "Successfully added a new entry.");

				$this->redirect($this->view->url->linkTo('crud_entity', array('name' => $c_entity)));
					
			}catch(\gnomephp\input\DataFieldValidException $e){
				\gnomephp\message\Message::add(\gnomephp\message\Message::ERROR, implode('<br />',$e->getErrors()));
			}
		}
		$this->view->render('crud/create');
	}



	protected function getClassName($object = null){
		if (!is_object($object) && !is_string($object)) {
			return false;
		}
			
		$class = explode('\\', (is_string($object) ? $object : get_class($object)));
		return $class[count($class) - 1];
	}


}